/**
 * 
 */
package cz.cvut.fit.crhonjar.mi.paa.dunedin.solver;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.TreeSet;

import cz.cvut.fit.crhonjar.mi.paa.dunedin.Bucket;
import cz.cvut.fit.crhonjar.mi.paa.dunedin.State;
import cz.cvut.fit.crhonjar.mi.paa.dunedin.exception.DunedinException;

/**
 * @author Jarec
 * 
 */
public class RankingSolver extends Solver {

	private final TreeSet<Integer> finals = new TreeSet<Integer>();

	@Override
	public void initializeQue() {
		final Comparator<State> c = new Comparator<State>() {

			@Override
			public int compare(final State s1, final State s2) {
				int rank1 = 0;
				int rank2 = 0;

				for (final Bucket b : s1.getBuckets()) {
					if (b.getActual() == b.getFinalVolume()) {
						rank1 += 6;
					} else if (RankingSolver.this.finals
							.contains(b.getActual())) {
						rank1 += 2;
					}

				}

				for (final Bucket b : s2.getBuckets()) {
					if (b.getActual() == b.getFinalVolume()) {
						rank2 += 6;
					} else if (RankingSolver.this.finals
							.contains(b.getActual())) {
						rank2 += 2;
					}
				}
				return rank2 - rank1;
			}

		};
		this.que = new PriorityQueue<State>(1024, c);
	}

	@Override
	public void solve() throws IllegalAccessException, DunedinException {

		for (final Bucket b : this.getStartingState().getBuckets()) {
			this.finals.add(b.getFinalVolume());
		}
		super.solve();
	}

}
